import sampleCovariance from "./sample_covariance.js";
import sampleStandardDeviation from "./sample_standard_deviation.js";

/**
 * [相关系数](http://en.wikipedia.org/wiki/Correlation_and_dependence)是衡量两个数据集相关性的指标，取值范围在-1到1之间
 *
 * @param {Array<number>} x 第一个输入数据集
 * @param {Array<number>} y 第二个输入数据集
 * @returns {number} 样本相关系数
 * @example
 * sampleCorrelation([1, 2, 3, 4, 5, 6], [2, 2, 3, 4, 5, 60]).toFixed(2);
 * // => '0.69'
 */
function sampleCorrelation(x, y) {
    const cov = sampleCovariance(x, y);
    const xstd = sampleStandardDeviation(x);
    const ystd = sampleStandardDeviation(y);

    return cov / xstd / ystd;
}

export default sampleCorrelation;
